package com.cbd.dao.impl;

import java.util.Date;
import java.util.List;
import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;
import com.cbd.dao.RentInfoDao;
import com.cbd.pojo.PageBean;
import com.cbd.pojo.RentInfo;

@Repository
public class RentInfoDaoImpl implements RentInfoDao {

	@Resource(name="sessionFactory")
	private SessionFactory sessionFactory;

	@Override
	public int updateRentInfoFk(int portId) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public List<RentInfo> findAllRentInfoByLadyId() {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from RentInfo");
		List<RentInfo> list  = query.list();
		return list;
	}

	@Override
	public RentInfo findRentInfoById(int infoId) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from RentInfo ri where ri.rentInfoID = ?");
		query.setParameter(0, infoId);
		return (RentInfo) query.list().get(0);
	}

	@Override
	public List<RentInfo> findAllIsDealInfo() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<RentInfo> findAllIsDealInfoByDate(Date startTime, Date endTime) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public PageBean findAllRentInfo(PageBean page,int ladyID) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from RentInfo r where r.rentInfoID not in (select distinct(p.rentInfo.rentInfoID) from Purpose p where p.isDeal = true) and r.personalcarport.landlady.ladyID = ?");
		int num = (page.getCurPage()-1)*page.getPageSize();
		query.setFirstResult(num);
		query.setMaxResults(page.getPageSize());
		query.setInteger(0, ladyID);
		List<RentInfo> list = query.list();
		for (RentInfo r : list) {
			r.setPurposes(null);
		}
		page.setList(list);
		return page;
	}

	@Override
	public int findRentInfoNum(int ladyID) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("select count(*) from RentInfo r where r.rentInfoID not in (select distinct(p.rentInfo.rentInfoID) from Purpose p where p.isDeal = true) and r.personalcarport.landlady.ladyID = ?");
		query.setInteger(0, ladyID);
		Long l =(Long) query.uniqueResult();
		return l.intValue();
	}

	@Override
	public int checkIsPublish(int carportID) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("select count(*) from RentInfo r where r.personalcarport.carportID = ?");
		query.setInteger(0, carportID);
		Long l = (Long) query.uniqueResult();
		return l.intValue();
	}

	@Override
	public void updateInfoTypeById(int infoID) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("update RentInfo r set r.type = false where r.rentInfoID = ?");
		query.setInteger(0, infoID);
		query.executeUpdate();
	}

}
